package org.mii.safeguard.v1.security.service;

import java.util.List;

import org.mii.safeguard.v1.security.dao.AuthorityDao;
import org.mii.safeguard.v1.security.entity.Authority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.hibernate.EntityManager;

/**
 * 授权管理类.
 * 
 * @author calvin
 */
@Service
@Transactional
public class AuthorityManager extends EntityManager<Authority, Long> {
	@Autowired
	private AuthorityDao authorityDao;

	@Override
	protected AuthorityDao getEntityDao() {
		return authorityDao;
	}

	public void labelAuthorited(List<Authority> allAuths,
			List<Long> authoritedIds) {
		if (allAuths == null || authoritedIds == null)
			return;

		for (Authority auth : allAuths) {
			auth.setIsAuth("false");
		}

		for (Authority auth : allAuths) {
			for (Long id : authoritedIds) {
				// 标记该权限初始时是否已经授权给了指定的role
				if (auth.getId() == id) {
					auth.setIsAuth("true");
					break;
				}
			}
		}
	}
}
